package com.guoxin.concurrentprogramming.chapter3.threadpool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 拒绝策略
 *
 * @author guoxin
 * @date 2019/7/16
 */
public class RejectThreadPoolDemo {

    private static class MyTask extends Thread {

        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + ":Thread ID:" + Thread.currentThread().getId());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //自定义线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MICROSECONDS,
                new LinkedBlockingQueue<>(10), (r, executor1) -> System.out.println(r.toString() + " is discard"));

        MyTask task = new MyTask();

        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            executor.submit(task);
            Thread.sleep(10);
        }
    }
}
